using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Configuration;
using SimpleReminder.Properties;
using SimpleUtils;

namespace SimpleReminder
{
    public partial class frmOptions : frmBase
    {
        public frmOptions()
        {
            InitializeComponent();

            InitForm();
        }

        public void InitForm()
        {
            int period = (int)(Settings.Default.CheckUpdatePeriod / 7) - 1;
            if (period < 0 || period > 3) period = 1;
            
            cboCheckUpdatePeriod.SelectedIndex = period;            
            chkCheckUpdateEnabled.Checked = Settings.Default.CheckUpdateEnabled;

            txtServer.Text = Settings.Default.SmtpServer;
            txtUserName.Text = Settings.Default.SmtpUserName;
            txtPassword.Text = Settings.Default.SmtpPassword;
            txtSenderName.Text = Settings.Default.SmtpSenderName;
            txtSenderEmail.Text = Settings.Default.SmtpSenderEmail;

            nudInterval.Value = Settings.Default.TimerInterval;

            chkAutoStartup.Checked = Settings.Default.AutoStartup;
            chkStartupMinimized.Checked = Settings.Default.StartupMinimized;

            chkLastLastRunTime.Checked = Settings.Default.ShowLastLastRunTime;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Settings.Default.CheckUpdateEnabled = chkCheckUpdateEnabled.Checked;
            Settings.Default.CheckUpdatePeriod = (cboCheckUpdatePeriod.SelectedIndex + 1) * 7;

            Settings.Default.SmtpServer = txtServer.Text;
            Settings.Default.SmtpUserName = txtUserName.Text;
            Settings.Default.SmtpPassword = txtPassword.Text;
            Settings.Default.SmtpSenderName = txtSenderName.Text;
            Settings.Default.SmtpSenderEmail = txtSenderEmail.Text;

            Settings.Default.TimerInterval = (int) nudInterval.Value;

            Settings.Default.AutoStartup = chkAutoStartup.Checked;
            Settings.Default.StartupMinimized = chkStartupMinimized.Checked;

            Settings.Default.ShowLastLastRunTime = chkLastLastRunTime.Checked;

            Settings.Default.Save();

            Program.TimerInterval = Settings.Default.TimerInterval;

            if (Settings.Default.AutoStartup)
            {
                SimpleWsh.CreateShortcut(SimpleWsh.SpecialFolders.Startup, Config.AppName, Application.ExecutablePath);
            }
            else
            {
                SimpleWsh.DeleteShortcut(SimpleWsh.SpecialFolders.Startup, Config.AppName);
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void chkCheckUpdateEnabled_CheckedChanged(object sender, EventArgs e)
        {
            cboCheckUpdatePeriod.Enabled = chkCheckUpdateEnabled.Checked;
        }

    } // end of class
}